package ui 
{
	import Base.Loader.SWFLoader;
	
	import Common.RenderManager;
	
	import flash.display.Loader;
	import flash.events.MouseEvent;
	import flash.net.SharedObject;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	/**
	 * ...
	 * @author ...
	 */
	public class PhotoUIHandler extends uiBase
	{
		public var sharedObject:SharedObject = null;
		public var page:int = 1;
		public var maxPage:int = 100;
		public var itemNum:int = 0;
		
		public var loader:Loader;
		
		public function PhotoUIHandler() 
		{
			path = "photo.swf";
			name = "PhotoUI";
		}
		
		
		public override function onInit( l:SWFLoader ):void
		{
			super.onInit( l );
			
			
			loader = new Loader();
			uiMc.petImg.addChild( loader );
			
			
			itemNum = uiMc.itemArray.length;
			for ( var i:int = 0 ; i < uiMc.itemArray.length ; i++ )
			{
				uiMc.itemArray[i].btn.addEventListener( MouseEvent.CLICK , onClick( i ) );
			}
			
			uiMc.closeBtn.addEventListener( MouseEvent.CLICK , onClose );
			uiMc.preBtn.addEventListener( MouseEvent.CLICK , onPre );
			uiMc.nextBtn.addEventListener( MouseEvent.CLICK , onNext );
			
			updateData();
		}
		
		
		public override function resize():void
		{
			if ( uiMc )
			{
				uiMc.x = uiMc.px + 15;
				uiMc.y = RenderManager.instance.stageHeight * 0.1 + uiMc.py;
			}
		}
		
		
		public function onClose( e:MouseEvent ):void
		{
			visible( false );
			
			uiMc.clear();
			loader.unload();
		}
		
		
		public function onClick( i:int ):Function
		{
			return function foo( e:MouseEvent ):void
			{
				if ( sharedObject )
				{
					var id:int = itemNum * ( page - 1 ) + i + 1;
					var obj:Object = sharedObject.data[ id ];
					
					if ( obj )
					{
						uiMc.setText( obj.n , obj.l , obj.h , obj.s , obj.t , obj.d );
						loader.load( new URLRequest( "../monster/" + obj.p + ".png" ) );
					}
				}
			}
		}
		
		public function onPre( e:MouseEvent ):void
		{
			if ( page == 1 )
			{
				return;
			}
			
			page--;
			
			updateData();
		}
		
		public function onNext( e:MouseEvent ):void
		{
			if ( page == maxPage )
			{
				return;
			}
			
			page++;
			
			updateData();
		}
		
		public function updateData():void
		{
			loader.unload();
			
			uiMc.clear();
			
			uiMc.setPage( page );
			
			sharedObject = null;
			sharedObject = SharedObject.getLocal( "foxSA" );
			
			if ( sharedObject )
			{
				for ( var i:int = 0 ; i < itemNum ; i++ )
				{
					var id:int = itemNum * ( page - 1 ) + i + 1;
					var obj:Object = sharedObject.data[ id ];
					if ( obj )
					{
						uiMc.setItem( i , id , obj.n );
					}
				}
			}
			
		}
		
		
		public override function visible( b:Boolean ):void
		{
			super.visible( b );
			
			if ( uiMc )
			{
				updateData();
			}
		}
		
		
		public static var instance:PhotoUIHandler = null;
		public static function Instance():PhotoUIHandler
		{
			if ( instance == null )
			{
				instance = new PhotoUIHandler();
			}
			
			return instance;
		}
		
	}

}